Note: rosrecord is being replaced by rosbag. The rosbag tutorial may be more appropriate.

Note: This tutorial assumes that you have completed the previous tutorials: Recording and playing back data.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Producing filtered bag files using rosrebag

Description: This tutorial will cover using rosrebag to filter bag files into new bag files using topic and data information

Keywords: rosrecord,rosplay,rosrebag

Tutorial Level: INTERMEDIATE

In the previous tutorial we saw how rosrecord could be used to only record a subset of the arguments. But suppose that you or someone else has made a bag file using rosrecord -a on a running system, logging all topics to a single bag file. You would like to test one of the nodes in this system using the an input stream from the bag file. But you do not also want to replay the messages that your test node emitted at the time the recording was made. Nor do you want to inject all the other old messages from the bagfile into your system. The command rosrebag exists to produce new bag files from old bag files using python filter expressions.

To see an example of this, either execute section 1 of the previous tutorial in order to create a bag file using turtle_teleop, or move to the directory that contains the bag file created during this tutorial. Running rosplay -c on the bag file should show both the input into turtlesim (/turtle1/command_velocity) and also the outputs of the turtlesim node (/turtle1/pose and /turtle1/color_sensor). We are going to make a new bag file containing only the input to turtlesim.

The command we use to manipulate existing bag files is called rosrebag:

Usage: rosrebag in.bag out.bag filter-expression

filter-expression can be any Python-legal expression.
The following variables are available:
 * topic: name of topic
 * m: message
 * t: time of message (t.secs, t.nsecs)

To create our new bag file we want to filter on the basis of topic name - we can see from the usage instructions that the variable "topic" can be used in the filtered expression. We want to filter the messages which have a topic name of /turtle1/command_velocity. Execute the following command:

rosrebag <your bagfile> turtlecom.bag 'topic == "/turtle1/command_velocity"'

Now run rosplay -c on turtlecom.bag. You should see a single topic for command_velocity.

The python filter functionality can extend beyond separating out messages by topic name - we can also filter based on the actual data in the message. For instance, suppose that my original teleop of turtlesim produced the following behavior:

  • jagged.png

Suppose further that we want to test a control algorithm on only left turns in turtlesim. The following command will pass turtlecom.bag through a filter that removes any references to commands with right (negative in angular velocity) values:

rosrebag turtlecom.bag left_only.bag "m.angular >= 0"

The result if we play it through turtlesim exhibits only left turns:

  • left.png

Wiki: rosrecord/Tutorials/Producing filtered bag files using rosrebag (last edited 2010-01-14 20:15:23 by JeremyLeibs)